﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using vnFingerPrint.BusinessObject;
using vnCommon;
namespace vnFingerPrint.DataAccess
{
    public static class Config_DataAccess
    {
        //Select Config
        public static List<Config_Object> SelectAll()
        {
            string SelectQuery = @"Select * from [Config]";
            List<Config_Object> result = new List<Config_Object>();
            DataTable tb = commonDatabase.GetSQLDataTable(clsConnectionString.GetConnectionString(), SelectQuery);
            foreach (DataRow row in tb.Rows)
            {
                Config_Object newObject = new Config_Object();
                newObject.ConfigName = (string)row["ConfigName"];
                if (row["ConfigValue"].ToString() != "") newObject.ConfigValue = (string)row["ConfigValue"];
                result.Add(newObject);
            }
            return result;
        }
        //Insert Config
        public static int Insert(Config_Object newObject)
        {
            string InsertQuery = "Insert Into [Config]";
            List<SqlParameter> paralist = new List<SqlParameter>();
            string _val = "", _para = "";
            _val += "ConfigName, "; _para += "@ConfigName, ";
            paralist.Add(new SqlParameter("ConfigName", newObject.ConfigName));
            if (newObject.ConfigValue != null)
            {
                _val += "ConfigValue, "; _para += "@ConfigValue, ";
                paralist.Add(new SqlParameter("ConfigValue", newObject.ConfigValue));
            }
            InsertQuery += "(" + _val + ") Values (" + _para + ")";
            InsertQuery = InsertQuery.Replace(", )", ")");
            return commonDatabase.ExecuteNonQuerySQL(clsConnectionString.GetConnectionString(), InsertQuery, paralist);
        }
        //Get an Object of Config
        public static Config_Object GetObject(string _ConfigName)
        {
            List<SqlParameter> paralist = new List<SqlParameter>();
            Config_Object newObject = new Config_Object();
            string SelectQuery = "Select * from [Config] where (1=1) and [ConfigName]= @ConfigName";
            paralist.Add(new SqlParameter("ConfigName", _ConfigName));
            DataTable tb = commonDatabase.GetSQLDataTable(clsConnectionString.GetConnectionString(), SelectQuery, paralist);
            if (tb.Rows.Count < 1) return null;
            DataRow row = tb.Rows[0];
            newObject.ConfigName = (string)row["ConfigName"];
            if (row["ConfigValue"].ToString() != "") newObject.ConfigValue = (string)row["ConfigValue"];
            return newObject;
        }
        //Save changes of an Object to database Config
        public static int SaveChanged(Config_Object UpdatingObject)
        {
            List<SqlParameter> paralist = new List<SqlParameter>();
            string UpdateQuery =
            @"Update [systbModules] SET
               ConfigValue= @ConfigValue
            Where ConfigName= @ConfigName";
            paralist.Add(new SqlParameter("ConfigName", UpdatingObject.ConfigName));
            paralist.Add(new SqlParameter("ConfigValue", UpdatingObject.ConfigValue));
            return commonDatabase.ExecuteNonQuerySQL(clsConnectionString.GetConnectionString(), UpdateQuery, paralist);
        }
        //Delete an Object in database Config
        public static int Delete(Config_Object DeletingObject)
        {
            List<SqlParameter> paralist = new List<SqlParameter>();
            string DeleteQuery =
            @"Delete [Config]
            Where ConfigName= @ConfigName";
            paralist.Add(new SqlParameter("ConfigName", DeletingObject.ConfigName));
            return commonDatabase.ExecuteNonQuerySQL(clsConnectionString.GetConnectionString(), DeleteQuery, paralist);
        }
        public static string GetUserImagePath(string fileName)
        {
            string UsersImageUrl = Config_DataAccess.GetObject("UsersImageFolder").ConfigValue;
            string url = @UsersImageUrl + @"\" + fileName;
            return url;
        }

        public static string GetCongDanImagePath(string fileName)
        {
            string UsersImageUrl = Config_DataAccess.GetObject("CitizensImageFolder").ConfigValue;
            string url = @UsersImageUrl + @"\" + fileName;
            return url;
        }

        public static string GetVanTayImagePath(string fileName)
        {
            string UsersImageUrl = Config_DataAccess.GetObject("FingerprintsImageForlder").ConfigValue;
            string url = @UsersImageUrl + @"\" + fileName;
            return url;
        }
    }
}